package org.wheel.module.core.request;

import lombok.Data;
import org.wheel.module.core.security.SqlSecurityUtils;

@Data
public class DynParam {
    private String apiID;
    private ApiParam apiParam;

    public static DynParam instance(String apiID) {
        DynParam dynParam = new DynParam();
        dynParam.setApiID(apiID);
        dynParam.setApiParam(ApiParam.instance());
        return dynParam;
    }

    public DynParam addFieldsParam(String fields) {
        if (fields == null || fields.isEmpty()) {
            return this;
        }
        String[] fa = fields.split(",");
        for (String f : fa) {
            ParamField field = new ParamField();
            field.setField(SqlSecurityUtils.escapeSqlIdentifier(f));
            apiParam.getFields().add(field);
        }
        return this;
    }

    public DynParam addFieldsParam(String field, Object value) {
        if (field == null || field.isEmpty()) {
            return this;
        }
        ParamField f = new ParamField();
        f.setField(SqlSecurityUtils.escapeSqlIdentifier(field));
        f.setValue(value);
        apiParam.getFields().add(f);
        return this;
    }

    public DynParam addWhereParam(String field, Object value) {
        ParamField where = new ParamField();
        where.setField(SqlSecurityUtils.escapeSqlIdentifier(field));
        where.setValue(value);
        apiParam.getWhere().add(where);
        return this;
    }

    public DynParam addWhereParam(String field, String connector, Object value) {
        ParamField where = new ParamField();
        where.setField(SqlSecurityUtils.escapeSqlIdentifier(field));
        where.setConnector(connector);
        where.setValue(value);
        apiParam.getWhere().add(where);
        return this;
    }

    public DynParam addWhereFuncParam(String field, String func) {
        ParamField where = new ParamField();
        where.setField(SqlSecurityUtils.escapeSqlIdentifier(field));
        where.setFunc(func);
        apiParam.getWhere().add(where);
        return this;
    }
}
